﻿using BaoVeThanhDia.GamePlay.Towers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;


namespace BaoVeThanhDia.GamePlay.UpGrade
{
    class UpgradeTowerButton
    {
        private Tower tower; // tower đang xét
        private int TypeButton;
        /* 0 là nâng cấp loại 1
         * 1 là nâng cấp loại 2
         * 2 là nâng cấp thường
         * 3 là bán tháp
         */
        private int Price;
        private Texture2D Imagenen;
        private Vector2 position;
        private Vector2 size;
        private float scale;
        private Vector2 lechPosi;
        private bool isVisible;
        private bool isTouch;
        public UpgradeTowerButton(Tower tower, int TypeButton)
        {
            isVisible = false;
            this.tower = tower;
            this.TypeButton = TypeButton;
            position = new Vector2(tower.getIndex().Y + 0.2f, tower.getIndex().X + 0.2f) * 128;
            if (TypeButton == 3) scale = 0.8f;
            else
                scale = 2.3f;
            size = new Vector2(140, 140) * GamePage.Scale;
            lechPosi = getDolech();
        }
        private Vector2 getDolech()
        {
            int temp = 80;
            if (TypeButton == 0) return new Vector2(-110, -100);
            else if (TypeButton == 1) return new Vector2(90, -100);
            else if (TypeButton == 2) return new Vector2(-90, 70);
            else return new Vector2(temp, temp);
        }
        public void Load(ContentManager cont)
        {
            if (TypeButton == 3)
            {
                Imagenen = cont.Load<Texture2D>("images/upgradeTower/gold");
            }
            else
            {
                Imagenen = cont.Load<Texture2D>("images/Introduction/border");
            }
        }
        public void Draw(SpriteBatch sprite, SpriteFont font, ContentManager cont)
        {
            setVisible();
            if (isVisible)
            {
                if (TypeButton == 3)
                    sprite.Draw(Imagenen, (position + lechPosi) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, TypeButton != 3 ? 114 : 101, TypeButton != 3 ? 114 : 101), Color.White, 0, Vector2.Zero, (isTouch ? scale + 0.2f : scale) * GamePage.Scale, SpriteEffects.None, 0);
                else
                {
                    sprite.Draw(Imagenen, (position + lechPosi) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 45, 45), Color.White, 0, Vector2.Zero, (isTouch ? scale + 0.2f : scale) * GamePage.Scale, SpriteEffects.None, 0);
                    if (TypeButton == 0)
                        tower.DrawPictureUpGrade(tower.getLevel() + 1, 1, cont, sprite, (position + lechPosi) * GamePage.Scale);
                    else if (TypeButton == 1)
                        tower.DrawPictureUpGrade(tower.getLevel() + 1, 2, cont, sprite, (position + lechPosi) * GamePage.Scale);
                    else if (TypeButton == 2)
                        tower.DrawPictureUpGrade(tower.getLevel() + 1, 0, cont, sprite, (position + lechPosi) * GamePage.Scale);
                }
                sprite.DrawString(font, Price.ToString(), (position + lechPosi + new Vector2(50, 50)) * GamePage.Scale + GamePage.Dolech, Color.White);
            }
        }
        public bool isTouched(Vector2 touch)
        {
            isTouch = false;
            if (!isVisible) { return false; }
            Vector2 posi = touch - GamePage.Dolech;
            if (posi.X >= (position.X + lechPosi.X) * GamePage.Scale && posi.X <= ((position.X + lechPosi.X) * GamePage.Scale + size.X * GamePage.Scale) && posi.Y >= (position.Y + lechPosi.Y) * GamePage.Scale && posi.Y <= ((position.Y + lechPosi.Y) * GamePage.Scale + size.Y * GamePage.Scale))
            {
                isTouch = true;
                return true;
            }
            else
            {
                return false;
            }
        }
        public void setVisible()
        {
            #region nút bán luôn luôn hiển thị
            if (TypeButton == 3)
            {
                Price = (int)(tower.getPriceBought() * 0.5f);
                isVisible = true;
                return;
            }
            #endregion
            else
            {
                #region nút nâng cấp tháp
                if (TypeButton == 2)
                {
                    if (tower.GetType() == typeof(BasicTower)) isVisible = false;
                    else if (tower.getLevel() != 1) isVisible = false;
                    else
                    {
                        Price = (int)tower.getPriceUpGrade(tower.getLevel() + 1, 0);
                        isVisible = true;
                    }
                }
                #endregion
                else // nút chia nhánh
                {
                    #region nếu là tháp cơ bản
                    if (tower.GetType() == typeof(BasicTower))
                    {
                        if (tower.getLevel() == 3) isVisible = false;
                        else if (tower.getLevel() == 1)
                        {
                            Price = (int)tower.getPriceUpGrade(tower.getLevel() + 1, TypeButton + 1);
                            isVisible = true;
                        }
                        else if (tower.getLevel() == 2 && TypeButton + 1 == tower.getType())
                        {
                            Price = (int)tower.getPriceUpGrade(tower.getLevel() + 1, TypeButton + 1);
                            isVisible = true;
                        }
                        else isVisible = false;
                    }
                    #endregion
                    else
                    {
                        #region các tháp còn lại
                        if (tower.getLevel() == 4) isVisible = false;
                        else if (tower.getLevel() == 1) isVisible = false;
                        else if (tower.getLevel() == 2)
                        {
                            Price = (int)tower.getPriceUpGrade(tower.getLevel() + 1, TypeButton + 1);
                            isVisible = true;
                        }
                        else if (tower.getLevel() == 3 && TypeButton + 1 == tower.getType())
                        {
                            Price = (int)tower.getPriceUpGrade(tower.getLevel() + 1, TypeButton + 1);
                            isVisible = true;
                        }
                        else isVisible = false;
                        #endregion
                    }
                }
                if (GamePage.Money < Price) isVisible = false; // xem đủ tiền để mua không
            }
        }
        public int getTypeButton() { return TypeButton; }
        public int getPrice() { return Price; }
    }
}
